package com.young.coderule.configuration;

import com.young.coderule.common.aspect.CodeRulesAspect;
import com.young.coderule.common.lock.LocalLock;
import com.young.coderule.common.lock.MyLock;
import com.young.coderule.common.lock.RedisLock;
import com.young.coderule.common.scan.CodeRuleResourceFactory;
import com.young.coderule.common.scan.CodeRuleResourceScaner;
import com.young.coderule.common.util.SpringContextUtil;
import com.young.coderule.controller.ComCodeRuleController;
import com.young.coderule.controller.ComCodeRuleNumberController;
import com.young.coderule.service.CodeRuleCacheService;
import com.young.coderule.service.ComCodeRuleNumberService;
import com.young.coderule.service.ComCodeRuleService;
import com.young.coderule.service.ComCodeRuleTService;
import com.young.coderule.service.impl.*;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;

/**
 * @DESCRIPTION: 编码规则自动装配类
 * @USER: Young
 */

@Configuration
/*开启了aop，并依赖mybatisplus才开启*/
@ConditionalOnClass(name = {"com.baomidou.mybatisplus.extension.service.impl.ServiceImpl","org.springframework.context.annotation.AspectJAutoProxyRegistrar"})
@MapperScan("com.young.coderule.mapper")
public class CodeRuleAutoConfiguration {

    @Autowired
    private ApplicationContext applicationContext;

    /*注入锁，后续可判断是否对接了redis，换成redis锁*/
    @Bean
    @ConditionalOnClass(name = {"org.springframework.data.redis.core.RedisTemplate"})
    public MyLock redisLock(){
        return new RedisLock();
    }
    @Bean
    @ConditionalOnMissingBean(value = {MyLock.class})
    public MyLock myLock(){
        return new LocalLock();
    }

    /*注入编码规则缓存服务，后续可判断是否对接了redis，换成基于redis的缓存服务*/
    @Bean
    @ConditionalOnClass(name = {"org.springframework.data.redis.core.RedisTemplate"})
    public CodeRuleCacheService codeRuleRedisCacheService(){
        return new CodeRuleRedisCacheService();
    }
    @Bean
    @ConditionalOnMissingBean(value = {CodeRuleCacheService.class})
    public CodeRuleCacheService codeRuleMapCacheService(){
        return new CodeRuleMapCacheService();
    }

    /*注入切面类*/
    @Bean
    public CodeRulesAspect codeRulesAspect(){
        return new CodeRulesAspect();
    }

    /*注入后置处理器，用来扫描所有controller的@CodeRule方法*/
    @Bean
    public CodeRuleResourceScaner codeRuleResourceScaner(){
        return new CodeRuleResourceScaner();
    }

    /*注入编码规则资源工厂*/
    @Bean
    public CodeRuleResourceFactory codeRuleResourceFactory(){
        return new CodeRuleResourceFactory();
    }

    /*
    以下，注入service和controller
    */
    @Bean
//    @ConditionalOnBean(name = {"comCodeRuleNumberMapper"})
    public ComCodeRuleNumberService comCodeRuleNumberService(){
        return new ComCodeRuleNumberServiceImpl();
    }
    @Bean
//    @ConditionalOnBean(name = {"comCodeRuleTMapper"})
    public ComCodeRuleTService comCodeRuleTService(){
        return new ComCodeRuleTServiceImpl();
    }
    @Bean
//    @ConditionalOnBean(name = {"comCodeRuleMapper"})
    public ComCodeRuleService comCodeRuleService(){
        return new ComCodeRuleServiceImpl();
    }
    @Bean
//    @ConditionalOnBean(name = {"comCodeRuleNumberService"})
    public ComCodeRuleNumberController comCodeRuleNumberController(){
        return new ComCodeRuleNumberController();
    }
    @Bean
//    @ConditionalOnBean(name = {"comCodeRuleService"})
    public ComCodeRuleController comCodeRuleController(){
        return new ComCodeRuleController();
    }

    /*将容器上下文注入*/
    @PostConstruct
    public void setSpringContext(){
        SpringContextUtil.setApplicationContext(applicationContext);
    }

}
